# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from ui import ui_tcptestor
from tcptabui import tcp_tab
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s
    
# 控制UI表现
class tcptab_manager(object):
    def __init__(self, logic_mod):        
        #逻辑模块引用
        self.logic_mod = logic_mod
        #ui模块引用
        self.ui_mod = ui_tcptestor.Ui_tcptestorui()
        #主对话框        
        self.dlg = QtGui.QDialog()
        flags=QtCore.Qt.WindowMinMaxButtonsHint 
        self.dlg.setWindowFlags(flags)        
        self.dlg.show()
        #将主对话框设置给ui模块
        self.ui_mod.setupUi(self.dlg)
        self.ui_mod.tabWidget.clear()
        #信号连接
        self.ui_mod.tabWidget.tabCloseRequested.\
            connect(self.on_tab_closed)
        
    def add_new_connection(self, address, logic_mod):
        newtab = tcp_tab(self, address, logic_mod)
        self.ui_mod.tabWidget.addTab(newtab, _fromUtf8(""))
        self.ui_mod.tabWidget.setTabText(self.ui_mod.tabWidget.indexOf(newtab), \
                                  QtGui.QApplication.translate("tcptestorui",\
                                  "未连接", None, QtGui.QApplication.UnicodeUTF8))
        
    def on_new_clicked(self):
        self.add_new_connection("", None)
    
    def on_tab_closed(self, index):
        if (self.ui_mod.tabWidget.count()>1):
            self.ui_mod.tabWidget.widget(index).on_tab_closed()
            self.ui_mod.tabWidget.removeTab(index)            
    
 
        
    